
let cache_user;

const currentUser = function(){
    if(cache_user){
        return cache_user;
    }
    const userVal = localStorage.getItem("user");
    if(userVal){
        const user = JSON.parse(userVal)
        cache_user = user;
        return cache_user;
    }
    return null;
}

cache_user = currentUser();

const loginUser = function(user){
    const json = JSON.stringify(user);
    localStorage.setItem("user", json);
    cache_user = user
}

const logoutUser = function(){
    localStorage.removeItem("user");
    cache_user = null;
    return cache_user;
}


export {
    currentUser,
    loginUser,
    logoutUser
}